// pages/travel/travel.js
var lo =120.577315,la=30.029130,dlo,dla,that
Page({

  /**
   * 页面的初始数据
   */
  data: {
    longitude:120.577315,
    latitude:30.029130,
    scale:16,
    index:0,
    result:"先提交坐标，再完成挑战",
    points:[
      {
        name:"北京:天安门广场",
        longitude:116.397506,
        latitude:39.908878
      },{
        name:"上海:东方明珠广播电视塔",
        longitude:121.499711,
        latitude:31.239808
      },{
        name:"杭州:雷峰塔",
        longitude:120.148868,
        latitude:30.231746
      },{
        name:"广州:广州塔",
        longitude:113.324445,
        latitude:23.106459
      },{
        name:"拉萨:布达拉宫",
        longitude:91.118281,
        latitude:29.654847
      }
    ],
    pointList:[
      "北京:天安门广场",
      "上海:东方明珠广播电视塔",
      "杭州:雷峰塔",
      "广州:广州塔",
      "拉萨:布达拉宫"
    ]
  },
  onReady:function(e){
    that=this
    this.mapCtx=wx.createMapContext('myMap')
  },
  pickerChange:function(e){
    this.setData({
      index:e.detail.value
      })
  },
  getCenter:function(){
    this.mapCtx.getCenterLocation({
      success:function(res){
        lo=Math.round(res.longitude*1000000)/1000000.0
        la=Math.round(res.latitude*1000000)/1000000.0
        that.setData({
          longitude:lo,
          latitude:la
        })
      }
    })
  },
  compareTo:function(){
    dlo=Number(this.data.points[this.data.index].longitude)
    dla=Number(this.data.points[this.data.index].latitude)
    if(Math.abs(dlo-lo)<0.002 &&Math.abs(dla-la)<0.002){
      this.setData({
        result:"恭喜你!!挑战成功!!"
      })
    }else{
      this.setData({
        result:"挑战失败,请继续努力..."
      })
    }
  },
  zoomIn:function(){
    var s=this.data.scale
    if(s<20){
      s++
      this.setData({
        scale:s
      })
    }
  },
  zoomOut:function(){
    var s=this.data.scale
    if(s>3){
      s--
      this.setData({
        scale:s
      })
    }
  },
  transmit:function(){
    this.setData({
      longitude:this.data.point[this.data.index].longitude,
      latitude:this.data.point[this.data.index].latitude,
      scale:18
    })
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    console.log("travel onload")

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
    mapCtx = wx.createMapContext('myMap')
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    console.log("travel onshow")
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
    console.log("travel onhide")
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
    console.log("travel onunload")
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})